import java.awt.Color;
import java.awt.Graphics;
import java.util.Map;

import com.speljohan.rsbot.bot.Bot;
import com.speljohan.rsbot.event.events.ServerMessageEvent;
import com.speljohan.rsbot.event.listeners.PaintListener;
import com.speljohan.rsbot.event.listeners.ServerMessageListener;
import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.wrappers.RSObject;
import com.speljohan.rsbot.script.wrappers.RSTile;
import com.speljohan.rsbot.script.wrappers.RSTilePath;

public class AustinsMiner extends Script implements PaintListener,
		ServerMessageListener {
	public int yewId = 1309;
	int oresMined = 0;
	String rock = "";
	public boolean powerMode;
	public String status = "Starting up";
	public String oreName;
	RSTile[] bankToMine = { new RSTile(3254, 3421), new RSTile(3254, 3429),
			new RSTile(3264, 3429), new RSTile(3276, 3429),
			new RSTile(3285, 3424), new RSTile(3288, 3411),
			new RSTile(3290, 3404), new RSTile(3291, 3398),
			new RSTile(3292, 3385), new RSTile(3290, 3375),
			new RSTile(3285, 3366) };
	RSTile[] mineToBank = reversePath(bankToMine);
	RSTile bankTile = new RSTile(3252, 3420);
	RSTile mineTile = new RSTile(3285, 3366);
	int picks[] = { 1275, 1265, 1273, 1267, 1271 };
	int copper[] = { 11960, 11961, 11962 };
	int tin[] = { 11958, 11957, 11959 };
	int iron[] = { 11955, 11956 };
	boolean paints;

	public boolean depositOres() {
		if (!bank.isOpen()) {
			return false;
		}

		bank.depositAllExcept(picks);
		bank.close();

		return true;

	}

	@Override
	public String getAuthor() {
		return "Coder Austin";
	}

	public String getMining() {
		return oreName;
	}

	@Override
	public String getName() {
		return "Miner- Varrock East";
	}

	public int getRock() {
		if (getMining().equals("Copper")) {
			return random(11960, 11962);
		}
		if (getMining().equals("Tin")) {
			return random(11957, 11959);
		}
		if (getMining().equals("Iron")) {
			for (int i = 0; i <= iron.length; i++) {
				final int n = randoms(iron.length);
				return iron[n];
			}
		}
		return -1;
	}

	@Override
	public String getScriptCategory() {
		return "Mining";
	}

	@Override
	public String getScriptDescription() {
		String html = "<html>";
		html += "<head>";
		html += "<style type=\"text/css\"> hr {color: white} p {margin-left: 20px}</style>";
		html += "</head>";
		html += "<body>";
		html += "<center>";
		html += "<b><font size=\"4\" color=\"red\">" + getName() + " v"
				+ getVersion() + "</font></b><br>";
		html += "</center>";
		html += "<center><table border=\"0\"><tr><td colspan=\"2\"><center><font size=\"4\"><b>:: Script Settings ::</b></font></center></td></tr><tr><td colspan=\"2\"><hr></td></tr><tr><td>";
		html += "<tr><td><b>Ore: </b></td><td><center><select name=\"oreName\">";
		html += "<option>Copper";// barb village
		html += "<option>Tin";// catherby
		html += "<option>Iron";// draynor
		html += "</select></center></td></tr>";
		html += "<tr><td><b>Power Mine Mode:</b></td><td><center><input type=\"checkbox\" name=\"powerMode\" value=\"true\"><B>Yes</b></center></td></tr>";
		html += "<tr><td><b>Paint:</b></td><td><center><input type=\"checkbox\" name=\"paintMode\" value=\"true\"><B>Yes</b></center></td></tr>";
		html += "</table>";
		html += "</center></body>";
		html += "</html>";

		return html;
	}

	@Override
	public double getVersion() {
		return 1.0;
	}

	@Override
	public int loop() {
		getMining();

		if (isInventoryFull() && powerMode == true) {
			dropAllExcept(picks);
		}
		if (isInventoryFull() && powerMode == false) {

			if (distanceTo(bankTile) >= 4 && isInventoryFull()) {

				walkToBank();

			} else {
				openBank();
				depositOres();
			}
			return random(500, 750);

		}
		if (needToWalk() && !isInventoryFull() && distanceTo(mineTile) >= 8) {
			walkToMine();

		}
		if (getMyPlayer().getAnimation() == miningAnimation()) {

			return 2000;

		}
		final RSObject rock = findObject(getRock());
		if (rock == null) {

			return 800;
		}
		atObject(rock, "Mine");

		return random(1500, 1750);

	}

	public int miningAnimation() {
		if (getMining().equals("Copper")) {
			return 624;
		}
		if (getMining().equals("Tin")) {
			return 624;
		}
		if (getMining().equals("Iron")) {
			return 624;
		}
		return -1;
	}

	public boolean needToWalk() {
		if (!getMyPlayer().isMoving()) {
			return true;
		}

		if (distanceTo(getDestination()) <= random(4, 7)) {
			return true;
		}
		return false;

	}

	@Override
	public void onFinish() {
		Bot.getEventManager().removeListener(PaintListener.class, this);
		Bot.getEventManager().removeListener(ServerMessageListener.class, this);
		log("Thanks for using!");
		log("Ores Mined: " + oresMined);
		return;
	}

	public void onRepaint(final Graphics render) {
		if (wantsPaint()) {
			render.setColor(Color.GREEN);
			render.drawString("Ores Mined: " + oresMined, 50, 267);
			// render.drawString("Exp Gain: " + yewsCut * 175, 50, 287);
			// render.drawString(getStatus(), 50, 307);

		} else {
			Bot.getEventManager().removeListener(PaintListener.class, this);
		}
	}

	@Override
	public boolean onStart(final Map<String, String> args) {
		oreName = args.get("oreName");
		powerMode = args.get("powerMode") != null ? true : false;
		paints = args.get("paintMode") != null ? true : false;
		log("Austin's Miner successfully Started");
		if (oreName.equals("Copper")) {
			System.out.println("Copper Selected");
			rock = "Copper";
		}
		if (oreName.equals("Tin")) {
			System.out.println("Tin Selected");
			rock = "Tin";
		}
		if (oreName.equals("Iron")) {
			System.out.println("Iron Selected");
			rock = "Iron";
		}
		/*
		 * toBank = new RSTile[ ] { new RSTile(3106, 3433), new RSTile(3094,
		 * 3439), new RSTile(3094, 3454), new RSTile(3099, 3465), new
		 * RSTile(3100, 3480), new RSTile(3093, 3491) }; toArea = reversePath(
		 * toBank ); usesNPCBanking = false;
		 */
		return true;
	}

	public boolean openBank() {
		final RSObject bank = findObject(11402); // search for bank.
		if (bank == null) {
			return false; // did not find the bank.
		}

		return atObject(bank, "Use-quickly");

	}

	public int randoms(final int range) {
		return (int) (Math.random() * range);
	}

	public void serverMessageRecieved(final ServerMessageEvent e) {
		final String message = e.getMessage();
		if (message.contains("You manage to mine")) {
			oresMined++; // add one to logs.
		}
	}

	public boolean walkToBank() {
		final RSTile[] randomizedPath = randomizePath(mineToBank, 2, 2);
		final RSTilePath path = new RSTilePath(randomizedPath, this);
		try {
			return path.walkToEnd();
		} catch (final InterruptedException e) {

			e.printStackTrace();
		}
		return false;
	}

	public boolean walkToMine() {
		final RSTile[] randomizedPath = randomizePath(bankToMine, 2, 2);
		final RSTilePath path = new RSTilePath(randomizedPath, this);
		try {
			return path.walkToEnd();
		} catch (final InterruptedException e) {

			e.printStackTrace();
		}
		return false;
	}

	public boolean wantsPaint() {
		if (paints == true) {
			return true;
		} else {
			return false;
		}
	}

}